package com.gsi.webstore.platform.storefront.pipelet.orderprocess.basket;

import com.intershop.beehive.core.capi.pipeline.Pipelet;
import com.intershop.beehive.core.capi.pipeline.PipelineDictionary;
import com.intershop.beehive.foundation.quantity.Quantity;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.intershop.beehive.bts.capi.orderprocess.LineItem;

/**
 * 
 */
public class CheckBasketMaxItemsPerOrder extends Pipelet
{

    /**
     * Constant used to access the pipeline dictionary with key 'BasketMaxItemsPerOrder'
     */
    public static final String DN_BASKET_MAX_ITEMS_PER_ORDER = "BasketMaxItemsPerOrder";
    /**
     * Constant used to access the pipeline dictionary with key 'TotalQuantity'
     */
    public static final String DN_TOTAL_QUANTITY = "TotalQuantity";
    /**
     * Constant used to access the pipeline dictionary with key 'ProductLineItem'
     */
    public static final String DN_PRODUCT_LINE_ITEM = "ProductLineItem";
    /**
     * Constant used to access the pipeline dictionary with key 'NewQuantity'
     */
    public static final String DN_NEW_QUANTITY = "NewQuantity";
    /**
     * Constant used to access the pipeline dictionary with key 'OldQuantity'
     */
    public static final String DN_OLD_QUANTITY = "OldQuantity";
    /**
     * Constant used to access the pipeline dictionary with key 'ErrorBasketMaxItemsPerOrder'
     */
    public static final String DN_ERROR_BASKET_MAX_ITEMS_PER_ORDER = "ErrorBasketMaxItemsPerOrder";

    /**
     * The pipelet's execute method is called whenever the pipelets gets
     * executed in the context of a pipeline and a request. The pipeline
     * dictionary valid for the currently executing thread is provided as a
     * parameter.
     * 
     * @param dict
     *            The pipeline dictionary to be used.
     * @throws PipeletExecutionException
     *             Thrown in case of severe errors that make the pipelet execute
     *             impossible (e.g. missing required input data).
     */
    public int execute(PipelineDictionary dict)
    {        
        // lookup 'OldQuantity' in pipeline dictionary
        BigDecimal oldQuantity = (BigDecimal)dict.get(DN_OLD_QUANTITY);
        
        // lookup 'NewQuantity' in pipeline dictionary
        String newQuantity = (String)dict.get(DN_NEW_QUANTITY);
        int quantity = 0;
        
        if (newQuantity != null) {
            try {
                quantity = Integer.parseInt(newQuantity);
            } catch (NumberFormatException e) {}
        }
        
        // lookup 'TotalQuantity' in pipeline dictionary
        BigDecimal totalQuantity = (BigDecimal)dict.get(DN_TOTAL_QUANTITY);
        
        // lookup 'BasketMaxItemsPerOrder' in pipeline dictionary
        Integer basketMaxItemsPerOrder = (Integer)dict.get(DN_BASKET_MAX_ITEMS_PER_ORDER);
        
        if (totalQuantity != null && oldQuantity != null 
                        && totalQuantity.intValue() + quantity - oldQuantity.intValue() > basketMaxItemsPerOrder) {
            dict.put(DN_ERROR_BASKET_MAX_ITEMS_PER_ORDER, true);
            return PIPELET_ERROR;
        }

        return PIPELET_NEXT;
    }
}